package com.example.iqiyitest.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.iqiyitest.MainActivity;
import com.example.iqiyitest.R;
import com.example.iqiyitest.adapter.DaohangAdapter;
import com.example.iqiyitest.adapter.MovieListAdapter;
import com.example.iqiyitest.base.Constants;
import com.example.iqiyitest.info.FenleiInfo;
import com.example.iqiyitest.info.HomeInfo;
import com.example.iqiyitest.info.MovieDetailInfo;
import com.example.iqiyitest.utils.ImageLoader;
import com.iqiyi.movieksdk.sdk.exported.HttpResult;
import com.iqiyi.movieksdk.sdk.exported.IMsgReceiver;
import com.iqiyi.movieksdk.sdk.exported.MovieSdk;
import com.iqiyi.movieksdk.sdk.exported.OnResponseListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivityForMovie extends Activity implements OnClickListener, IMsgReceiver {
	private ImageLoader mImageLoader;
	ImageView home_img_1, home_img_2, home_img_3, home_img_4;
	RelativeLayout ly_1, ly_2, ly_3, ly_4;
	private MovieSdk movieSdk;
	private String host = "192.168.1.89";// 正式
	String zuixin_img,zuire_img,huayu_img,waiyu_img;
	String TAG = "iqiyitest";
//	ArrayList<HomeInfo> zuixinInfos, zuireInfos, waiyuInfos, huayuInfos;
	ArrayList<MovieDetailInfo> zuixinInfos, zuireInfos, waiyuInfos, huayuInfos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		mImageLoader = ImageLoader.getInstance();// zhangzhl--后进先出法 ，优先加载 最后一个
		home_img_1 = (ImageView) findViewById(R.id.home_img_1);
		home_img_2 = (ImageView) findViewById(R.id.home_img_2);
		home_img_3 = (ImageView) findViewById(R.id.home_img_3);
		home_img_4 = (ImageView) findViewById(R.id.home_img_4);
		ly_1 = (RelativeLayout) findViewById(R.id.ly_1);
		ly_2 = (RelativeLayout) findViewById(R.id.ly_2);
		ly_3 = (RelativeLayout) findViewById(R.id.ly_3);
		ly_4 = (RelativeLayout) findViewById(R.id.ly_4);
		movieSdk = MovieSdk.init(getApplication(), Constants.serviceUrl, new MovieSdk.OnInitListener() {
			@Override
			public void initOver(int code) {
				Toast.makeText(MainActivityForMovie.this, "init code:" + code, Toast.LENGTH_SHORT).show();
			}
		});
		movieSdk.addMsgReceiver(this);
		init();

	}

	public void showHomeImg(String data) {
		zuixinInfos = new ArrayList<>();
		zuireInfos = new ArrayList<>();
		huayuInfos = new ArrayList<>();
		waiyuInfos = new ArrayList<>();
		String type = "";
//		movieInfos = new ArrayList<>();
		JSONArray jsonArray;
		MovieDetailInfo movieDetailInfo;
		try {
			jsonArray = new JSONArray(data);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject;
				jsonObject = jsonArray.getJSONObject(i);
				Log.e(TAG,"首页111111"+ jsonObject.toString());
				type = jsonObject.optString("name");
				Log.e(TAG,"首页22222222"+type);
				ArrayList<MovieDetailInfo> movieDetailInfos = new ArrayList<>();
				JSONArray jsonArray2 = jsonObject.getJSONArray("episodes");
				Log.e(TAG,"首页333333"+ jsonArray2.toString());
				for (int j = 0; j < jsonArray2.length(); j++) {
					JSONObject jsonObject2;
					jsonObject2 = jsonArray2.getJSONObject(j);
					Log.e(TAG,"首页4444444444444"+ jsonObject2.toString());
					movieDetailInfo = new MovieDetailInfo();
					movieDetailInfo.setPosterLocalImg(jsonObject2.optString("posterLocalImg"));
					movieDetailInfo.setScore(jsonObject2.optInt("score"));
					movieDetailInfo.setPlayType(jsonObject2.optString("playType"));
					movieDetailInfo.setShotDesc(jsonObject2.optString("shotDesc"));
					movieDetailInfo.setQipuId(jsonObject2.optInt("qipuId"));
					movieDetailInfo.setDisplayName(jsonObject2.optString("displayName"));
					movieDetailInfo.setVideoType(jsonObject2.optString("videoType"));
					movieDetailInfos.add(movieDetailInfo);
				}
				switch (type) {
				case "最近热播":
					zuireInfos=movieDetailInfos;
					break;
				case "最新上线":
					zuixinInfos=movieDetailInfos;
					break;
				case "华语排行":
					huayuInfos=movieDetailInfos;
					break;
				case "外语排行":
					waiyuInfos=movieDetailInfos;
					break;
				default:
					break;
				}
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e(TAG, e.getMessage().toString());
		}
		//最新
		new Thread() {public void run() {
				try {
					 if(zuixinInfos!=null&&zuixinInfos.size()>0) {
						 for(int i=0;i<zuixinInfos.size();i++) {
							 Log.e(TAG+i, "轮播图1111111111111111");
							 zuixin_img=zuixinInfos.get(i).getPosterLocalImg();
							 Log.e(TAG+i, "轮播图1111"+zuixin_img);
							 Message m = new Message();
							 m.what=1;
							 zuixinHandler.sendMessage(m); 
							 if(i==zuireInfos.size()-1) {
								 i=0;
							 }
							 Thread.sleep(5000);
						 }
					 }
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		   }}.start();
		 //最热
			new Thread() {public void run() {
					try {
						 if(zuireInfos!=null&&zuireInfos.size()>0) {
							 for(int i=0;i<zuireInfos.size();i++) {
								 Log.e(TAG+i, "轮播图222222222222222222");
								 zuire_img=zuireInfos.get(i).getPosterLocalImg();
								 Message m = new Message();
								 m.what=1;
								 zuireHandler.sendMessage(m); 
								 if(i==zuireInfos.size()-1) {
									 i=0;
								 }
								 Thread.sleep(5000);
							 }
						 }
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			   }}.start();
			 //华语
				new Thread() {public void run() {
						try {
							 if(huayuInfos!=null&&huayuInfos.size()>0) {
								 for(int i=0;i<huayuInfos.size();i++) {
									 Log.e(TAG+i, "轮播图3333333333333333333");
									 huayu_img=huayuInfos.get(i).getPosterLocalImg();
									 Message m = new Message();
									 m.what=1;
									 huayuHandler.sendMessage(m); 
									 if(i==huayuInfos.size()-1) {
										 i=0;
									 }
									 Thread.sleep(5000);
								 }
							 }
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				   }}.start();
				 //外语
					new Thread() {public void run() {
							try {
								 if(waiyuInfos!=null&&waiyuInfos.size()>0) {
									 for(int i=0;i<waiyuInfos.size();i++) {
										 Log.e(TAG+i, "轮播图44444444444444444");
										 waiyu_img=waiyuInfos.get(i).getPosterLocalImg();
										 Message m = new Message();
										 m.what=1;
										 waiyuHandler.sendMessage(m); 
										 if(i==waiyuInfos.size()-1) {
											 i=0;
										 }
										 Thread.sleep(5000);
									 }
								 }
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
					   }}.start();
	}
	private Handler zuixinHandler = new Handler() {
	     public void handleMessage(Message msg) {
	       switch (msg.what) {
	       case 1:
	    	   Log.e("图片地址",zuixin_img);
	    	   try {
	   			mImageLoader.loadImage(zuixin_img, home_img_1,
	   					true);
	   		} catch (Exception e) {
	   			e.printStackTrace();
	   		}
	        break;
	       default:
	           break;
	       }
	          super.handleMessage(msg);
	     }
	};
	private Handler zuireHandler = new Handler() {
	     public void handleMessage(Message msg) {
	       switch (msg.what) {
	       case 1:
	    	   try {
	   			mImageLoader.loadImage(zuire_img, home_img_2,
	   					true);
	   		} catch (Exception e) {
	   			e.printStackTrace();
	   		}
	        break;
	       default:
	           break;
	       }
	          super.handleMessage(msg);
	     }
	};
	private Handler huayuHandler = new Handler() {
	     public void handleMessage(Message msg) {
	       switch (msg.what) {
	       case 1:
	    	   try {
	   			mImageLoader.loadImage(huayu_img, home_img_3,
	   					true);
	   		} catch (Exception e) {
	   			e.printStackTrace();
	   		}
	        break;
	       default:
	           break;
	       }
	          super.handleMessage(msg);
	     }
	};
	private Handler waiyuHandler = new Handler() {
	     public void handleMessage(Message msg) {
	       switch (msg.what) {
	       case 1:
	    	   try {
	   			mImageLoader.loadImage(waiyu_img, home_img_4,
	   					true);
	   		} catch (Exception e) {
	   			e.printStackTrace();
	   		}
	        break;
	       default:
	           break;
	       }
	          super.handleMessage(msg);
	     }
	};
	public void init() {
		ly_1.setOnClickListener(this);
		ly_2.setOnClickListener(this);
		ly_3.setOnClickListener(this);
		ly_4.setOnClickListener(this);
//		try {
//			mImageLoader.loadImage("http://192.168.1.241:80/image/20180504/v_112882553_m_600_m2_260_360.jpg",
//					home_img_2, true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		try {
//			mImageLoader.loadImage("http://192.168.1.241:80/image/20180412/v_111393030_m_600_m2_260_360.jpg",
//					home_img_3, true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		try {
//			mImageLoader.loadImage("http://192.168.1.241:80/image/20180412/v_109426865_m_600_260_360.jpg", home_img_4,
//					true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		movieSdk.sdk_homePage(1, 10, new OnResponseListener() {
			@Override
			public void onResponse(HttpResult result) {
				Log.d(TAG, result.data);
				showHomeImg(result.data);
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.ly_1:
			startActivity(new Intent(MainActivityForMovie.this, ShaixuanActivity.class).putExtra("selectTypeName", "最近热播"));
			break;
		case R.id.ly_2:
			startActivity(new Intent(MainActivityForMovie.this, ShaixuanActivity.class).putExtra("selectTypeName", "最新上线"));
			break;
		case R.id.ly_3:
			startActivity(new Intent(MainActivityForMovie.this, ShaixuanActivity.class).putExtra("selectTypeName", "华语排行"));
			break;
		case R.id.ly_4:
			startActivity(new Intent(MainActivityForMovie.this, ShaixuanActivity.class).putExtra("selectTypeName", "外语排行"));
			break;
		default:
			break;
		}
	}

	@Override
	public void onReceived(final String s) {
		Toast.makeText(this, "msg:" + s, Toast.LENGTH_SHORT).show();
		Log.d(TAG, "msg" + s);
	}
}
